package cn.mafangui.hotel.controller.common;

import cn.mafangui.hotel.entity.Hotel;
import cn.mafangui.hotel.response.AjaxResult;
import cn.mafangui.hotel.response.ResponseTool;
import cn.mafangui.hotel.service.HotelService;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//import net.sf.json.JSONArray;  //用于集合或数组
//import net.sf.json.JSONObject; //用于对象


@RestController
@RequestMapping(value = "/hotel")
public class HotelController {

    /*
    注入Service服务
     */
    @Autowired
    private final HotelService hotelService = null;

    @RequestMapping(value = "")
    public AjaxResult getAllHotel() {
        return ResponseTool.success(hotelService.selectAll());
    }

    @RequestMapping(value = "/stati")
    public AjaxResult getOrderStatic() {
        List<HashMap<String, Object>> hashMaps = hotelService.selectStatic();
        double[] data = new double[12];
        double total = 0;
        int month = 0;
        for (Map<String, Object> m : hashMaps) {
            for (String k : m.keySet()) {
                if (k.equals("month")) {
                    month = Integer.parseInt(String.valueOf(m.get(k)) );
                }
                if (k.equals("total")){
                    total = Double.parseDouble(String.valueOf(m.get(k)) );
                }
            }
            if (total != 0 & month != 0) {
                data[(month - 1)] = total;
            }
            total=0;
            month=0;
        }
        JSONArray json = JSONArray.fromObject(data);
        return ResponseTool.success(json);
    }

    @RequestMapping(value = "/ordernum")
    public AjaxResult getOrderNumStatic() {
        List<HashMap<String, Object>> hashMaps = hotelService.selectNum();
        double[] data = new double[12];
        int num = 0;
        int month = 0;
        for (Map<String, Object> m : hashMaps) {
            for (String k : m.keySet()) {
                if (k.equals("month")) {
                    month = Integer.parseInt(String.valueOf(m.get(k)) );
                }
                if (k.equals("num")){
                    num = Integer.parseInt(String.valueOf(m.get(k)) );
                }
            }
            if (num != 0 & month != 0) {
                data[(month - 1)] = num;
            }
            num=0;
            month=0;
        }
        JSONArray json = JSONArray.fromObject(data);
        return ResponseTool.success(json);
    }

    @RequestMapping(value = "/roomper")
    public AjaxResult getRoomPer() {
        List<HashMap<String, Object>> hashMaps = hotelService.selectPer();
        for (Map<String, Object> m : hashMaps) {
            for (String k : m.keySet())
                System.out.println(k +"  :  "+ m.get(k));
        }
        JSONArray json = JSONArray.fromObject(hashMaps);
        System.out.println(json);
        return ResponseTool.success(json);
    }

    @RequestMapping(value = "/orderper")
    public AjaxResult getOrderPer() {
        List<HashMap<String, Object>> hashMaps = hotelService.selectOrder();
        for (Map<String, Object> m : hashMaps) {
            for (String k : m.keySet())
                System.out.println(k +"  :  "+ m.get(k));
        }
        JSONArray json = JSONArray.fromObject(hashMaps);
        System.out.println(json);
        return ResponseTool.success(json);
    }
}
